<?php
/* pChart library inclusions */
include_once("../class/pData.class.php");
include_once("../class/pDraw.class.php");
include_once("../class/pImage.class.php");
include_once("../class/pScatter.class.php");

include_once("graph-settings.php");

global $xAxisLabels;

function xLabelConvertor($value) {
	global $xAxisLabels;
	return $xAxisLabels[$value];
}

function median()
{
	$args = func_get_args();

	switch(func_num_args())
	{
		case 0:
			trigger_error('median() requires at least one parameter',E_USER_WARNING);
			return false;
			break;

		case 1:
			$args = array_pop($args);
			// fallthrough

		default:
			if(!is_array($args)) {
				trigger_error('median() requires a list of numbers to operate on or an array of numbers',E_USER_NOTICE);
				return false;
			}

			sort($args);
			 
			$n = count($args);
			$h = intval($n / 2);

			if($n % 2 == 0) {
				$median = ($args[$h] + $args[$h-1]) / 2;
			} else {
				$median = $args[$h];
			}

			break;
	}
	 
	return $median;
}

function drawMyPlotGraph($serie, $serieIndexesToXLabels, $xLabels, $xAxisTitle,
		$yAxisTitle, $title, $optionalParams = array()) {
	
	$drawMedians = isset($optionalParams["DrawMedians"]) ? $optionalParams["DrawMedians"] : FALSE; 

	global $xAxisLabels;
	$xAxisLabels = $xLabels;
	
	/* Create the pData object */
	$myData = new pData();
	
	/* Create the X axis and the binded series */
	$myData->addPoints($serieIndexesToXLabels, "Serie 1");
	
	$myData->setAxisXY(0, AXIS_X);
	$myData->setAxisPosition(0, AXIS_POSITION_BOTTOM);
	
	/* Create the Y axis and the binded series */
	$myData->addPoints($serie, "Serie 1 values");
	
	$myData->setSerieOnAxis("Serie 1 values", 1);
	$myData->setAxisXY(1, AXIS_Y);
	$myData->setAxisPosition(1, AXIS_POSITION_LEFT);
	
	/* Create the 1st scatter chart binding */
	$myData->setScatterSerie("Serie 1", "Serie 1 values", 0);
	$myData->setScatterSerieColor(0, array("R"=>0,"G"=>0,"B"=>0));
	$myData->setScatterSerieShape(0, SERIE_SHAPE_CIRCLE);
	
	$rightRevervedSpace = $drawMedians ? 100 : 0;
 	$myPicture = prepareGraph($myData, $title, $xAxisTitle, $yAxisTitle, FALSE,
 			$drawMedians, $serie, $serieIndexesToXLabels, $xLabels);
 	
 	$myScatter = new pScatter($myPicture, $myData);
 	
 	$xLabelsCount = count($xLabels); 
	$yMax = max($serie);
 	
	/* Draw the scale */
	$scaleSettings = array(
			"XMargin"=>15,
			"YMargin"=>15,
			"Floating"=>TRUE,
			"GridR"=>200,
			"GridG"=>200,
			"GridB"=>200,
			"CycleBackground"=>TRUE,
			"XLabelsRotation"=>0,
			"ManualScale" => array(0 => array("Rows" => $xLabelsCount, "RowHeight" => 1, "Min" => 0, "Max" => $xLabelsCount - 1)),
			"ValueConvertor" => "xLabelConvertor",
			"Mode" => SCALE_MODE_START0);
	$myScatter->drawScatterScale($scaleSettings);
	
	/* Draw a scatter plot chart */
	$myPicture->Antialias = TRUE;
	$myScatter->drawScatterPlotChart();
	
	/* Render the picture (choose the best way) */
	$myPicture->autoOutput("plotgraph-$title.png");
}

function drawMedians($myPicture, $serie, $serieIndexesToXLabels, $xLabels, $x1, $y1, $x2, $y2) {

	// compute medians
	$medians = array();
	foreach ($xLabels as $i => $label) {
		$values = array();
		
		foreach ($serie as $j => $value) {
			$index = $serieIndexesToXLabels[$j];
			
			if ($index != $i) {
				continue;
			} 
			
			$values[] = $value;
		}
		
		$medians[$label] = median($values);
	}
	
	// compose text
	$text = "Medians:";
	foreach ($medians as $label => $median) {
		$text .= "\n" . $label . "   " . $median . " h";
	}
	
	$RectangleSettings = array("R"=>209,"G"=>198,"B"=>27,"Alpha"=>100,"Ticks"=>2); 
	$myPicture->drawRectangle($x1, $y1, $x2, $y2, $RectangleSettings);
	
	$xMiddle = ($x2 + $x1) / 2;
	$yMiddle = ($y2 + $y1) / 2;
	
	$TextSettings = array("Align" => TEXT_ALIGN_MIDDLEMIDDLE);
	$myPicture->drawText($xMiddle, $yMiddle, $text, $TextSettings);
}

function prepareGraph($graphData, $title, $xAxisName, $yAxisName, $useLegend,
		$drawMedians, $serie, $serieIndexesToXLabels, $xLabels) {
	
	$graphWidth = GRAPH_WIDTH;
	$graphHeight = GRAPH_HEIGHT;
	
	$titleVerticalSpace = 60;
	$legendVerticalSpace = $useLegend ? 30 : 0;
	$labelsVerticalSpace = 20;
	$xAxisNameSpace = 30;
	
	$leftMargin = 10;
	$yAxisNameSpace = 20; // should be determined by getTextBox
	$spaceBetweenYAxisAndName = 30;
	$leftHorizontalSpace = $leftMargin + $yAxisNameSpace + $spaceBetweenYAxisAndName;
	$rightHorizontalSpace = 35;
	
	$mediansHorizontalSpace = $drawMedians ? 150 : 0;
	$mediansHorizontalMargin = 5;
	
	$width = $graphWidth - $leftHorizontalSpace - $rightHorizontalSpace - $mediansHorizontalSpace - $mediansHorizontalMargin;
	$height = $graphHeight - $titleVerticalSpace - $legendVerticalSpace - $labelsVerticalSpace - $xAxisNameSpace;
	
	$x1 = $leftHorizontalSpace;
	$y1 = $titleVerticalSpace + $legendVerticalSpace;
	$x2 = $x1 + $width;
	$y2 = $y1 + $height;

	$titleFormat = array("FontName"=>"../../fonts/verdana.ttf", "FontSize"=>20, "Align"=>TEXT_ALIGN_MIDDLEMIDDLE);
	$xAxisNameFormat = array("FontName"=>"../../fonts/verdana.ttf", "FontSize"=>12, "Align"=>TEXT_ALIGN_MIDDLEMIDDLE);
	$yAxisNameFormat = array("FontName"=>"../../fonts/verdana.ttf", "FontSize"=>12, "Align"=>TEXT_ALIGN_MIDDLEMIDDLE, "Angle" => 90);
	$legendFormat = array("FontName"=>"../../fonts/Forgotte.ttf", "FontSize"=>14, "Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL);
	
	/* Create the pChart object */
	$myPicture = new pImage($graphWidth, $graphHeight, $graphData);
	$myPicture->setFontProperties(array("FontName"=>"../../fonts/Forgotte.ttf", "FontSize"=>14));
	
	/* Draw the background */
	$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
	$myPicture->drawFilledRectangle(0, 0, $graphWidth, $graphHeight, $Settings);
	
	/* Overlay with a gradient */
	$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
	$myPicture->drawGradientArea(0, 0, $graphWidth, $graphHeight, DIRECTION_VERTICAL,$Settings);
	
	/* Add a border to the picture */
	$myPicture->drawRectangle(0, 0, $graphWidth - 1, $graphHeight - 1, array("R"=>0,"G"=>0,"B"=>0));
	
	$xAxisLabelMiddleX = $leftHorizontalSpace + ($width / 2);
	$xAxisLabelMiddleY = $graphHeight - ($xAxisNameSpace / 2);
	$myPicture->drawText($xAxisLabelMiddleX, $xAxisLabelMiddleY, $xAxisName, $xAxisNameFormat);
	
	/* Write the y axis label */
	$yAxisLabelMiddleX = $leftMargin + ($yAxisNameSpace / 2);
	$yAxisLabelMiddleY = ($y1 + $y2) / 2;
	$myPicture->drawText($yAxisLabelMiddleX, $yAxisLabelMiddleY, $yAxisName, $yAxisNameFormat);
	
	/* Write the chart title */
	$myPicture->drawText($graphWidth / 2, $titleVerticalSpace / 2, $title, $titleFormat);
	
	$myPicture->setGraphArea($x1, $y1, $x2, $y2);
	$myPicture->drawFilledRectangle($x1, $y1, $x2, $y2, array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
	
	/* Write the legend */
	if ($useLegend) {
		$myPicture->drawLegend($leftHorizontalSpace, $titleVerticalSpace, $legendFormat);
	}
	
	if ($drawMedians) {
		$mediansX1 = $graphWidth - $mediansHorizontalSpace - $rightHorizontalSpace + $mediansHorizontalMargin; 
		$mediansY1 = $titleVerticalSpace;
		$mediansX2 = $graphWidth - $rightHorizontalSpace;
		$mediansY2 = $graphHeight - $legendVerticalSpace - $labelsVerticalSpace - $xAxisNameSpace;
		
		drawMedians($myPicture, $serie, $serieIndexesToXLabels, $xLabels,
				$mediansX1, $mediansY1, $mediansX2, $mediansY2);
	}
	
	return $myPicture;
}
?>
